﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using He.Common;
using He.Web.UI;

namespace He.Web.manage.links
{
    public partial class edit : He.Web.UI.Manage
    {
        private string action = Enums.ActionEnum.Add.ToString(); //操作类型
        public int id;
        public string pos_mark;
        public string imgurl = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            pos_mark = GetString("pos_mark");
            string _action = GetString("action");
            if (!string.IsNullOrEmpty(_action) && _action == Enums.ActionEnum.Edit.ToString())
            {
                this.action = Enums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    ShowParamError();
                    return;
                }
            }

            if (!IsPostBack)
            {
                if (action == Enums.ActionEnum.Edit.ToString())
                {
                    chkLoginLevel("edit" + pos_mark);

                    ShowInfo(this.id);
                }
                else
                {
                    chkLoginLevel("add" + pos_mark);
                }
            }
        }
        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            Model.links model = new BLL.links().GetModel(_id);
            txtTitle.Text = model.title;
            txtWebUrl.Text = model.web_url;
            txtUserName.Text = model.user_name;
            txtUserTel.Text = model.user_tel;
            txtUserMail.Text = model.user_email;
            txtImgUrl.Text = model.img_url;
            cblItem.Items[0].Selected = model.is_red == 1 ? true : false;
            txtSortId.Text = model.sort_id.ToString();

            imgurl = model.img_url;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            BLL.links bll = new BLL.links();
            Model.links model = new Model.links();
            model.title = txtTitle.Text.Trim();
            model.user_name = txtUserName.Text.Trim();
            model.user_tel = txtUserTel.Text.Trim();
            model.user_email = txtUserMail.Text.Trim();
            model.web_url = txtWebUrl.Text.Trim();
            model.img_url = txtImgUrl.Text.Trim();
            model.is_image = cbIsImage.Checked == true ? 1 : 0;
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.is_red = 0;
            if (cblItem.Items[0].Selected == true)
            {
                model.is_red = 1;
            }
            model.add_time = DateTime.Now.ToString();
            model.lang = lang;
            model.pos_mark = pos_mark;
            if (bll.Add(model) > 0)
            {
                SaveLogs(string.Format("[友情链接管理]增加内容:{0}", model.title));
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            BLL.links bll = new BLL.links();
            Model.links model = new BLL.links().GetModel(_id);
            model.title = txtTitle.Text.Trim();
            model.user_name = txtUserName.Text.Trim();
            model.user_tel = txtUserTel.Text.Trim();
            model.user_email = txtUserMail.Text.Trim();
            model.web_url = txtWebUrl.Text.Trim();
            model.img_url = txtImgUrl.Text.Trim();
            model.is_image = cbIsImage.Checked == true ? 1 : 0;
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.is_red = 0;
            if (cblItem.Items[0].Selected == true)
            {
                model.is_red = 1;
            }

            if (bll.Update(model))
            {
                SaveLogs(string.Format("[友情链接管理]修改内容:{0}", model.title));
                return true;
            }
            return false;
        }
        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {

            if (action == Enums.ActionEnum.Edit.ToString()) //修改
            {
                string url = CookieHelper.Get("LastListUrl");
                if (string.IsNullOrEmpty(url))
                {
                    url = Request.RawUrl;
                }

                if (!DoEdit(this.id))
                {
                    JscriptPrint("保存过程中发生错误啦！", url, "Error");
                    return;
                }
                JscriptPrint("修改信息成功啦！", url, "Success");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptPrint("保存过程中发生错误啦！", Request.RawUrl, "Error");
                    return;
                }
                JscriptPrint("添加信息成功啦！", Request.RawUrl, "Success");
            }


        }
    }
}
